package com.rjpacket.MyUEnjoy.adapters;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.rjpacket.MyUEnjoy.R;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import java.util.List;
import java.util.Map;

/**
 * Created by aaa on 15-1-23.
 */
public class StoreListBaseAdapter extends BaseAdapter {
    private Context ct;
    private List<Map<String,Object>> list;
    private ImageView imageView;

    public StoreListBaseAdapter(Context ct,List<Map<String,Object>> list){
        this.ct = ct;
        this.list = list;
    }
    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(ct);
        View view = inflater.inflate(R.layout.item_product, parent, false);
        imageView = (ImageView) view.findViewById(R.id.product_image);
        TextView id = (TextView) view.findViewById(R.id.product_id);
        TextView name = (TextView) view.findViewById(R.id.product_name);
        Map<String, Object> map = list.get(position);
        id.setText((String)map.get("id"));
        name.setText((String)map.get("name"));
        if(position%3 == 0){
            view.setBackgroundResource(R.drawable.a);
        }else if(position%3 == 1){
            view.setBackgroundResource(R.drawable.b);
        }else{
            view.setBackgroundResource(R.drawable.c);
        }
        ImageTask task = new ImageTask();
        task.execute("Http://doushangshang.com/"+map.get("goods_thumb"));
        return view;
    }

    private class ImageTask extends AsyncTask<String,Integer,Bitmap>{

        @Override
        protected Bitmap doInBackground(String... params) {
            Bitmap bitmap = null;
            String proUrl = null;
            if(params != null && params.length>0){
                proUrl = params[0];
                HttpGet request = new HttpGet(proUrl);
                HttpClient client = new DefaultHttpClient();

                try {
                    HttpResponse response = client.execute(request);
                    int state = response.getStatusLine().getStatusCode();
                    if(state == 200) {
                        HttpEntity entity = response.getEntity();
                        byte[] bytes = EntityUtils.toByteArray(entity);
                        bitmap = BitmapFactory.decodeByteArray(bytes,0,bytes.length);
                    }
                    }catch(Exception e){
                    e.printStackTrace();
                    }
                }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            if(bitmap != null){
                imageView.setImageBitmap(bitmap);
            }
        }
    }
}
